<template>
    <div>
        <el-drawer title="注册" :visible.sync="drawer" :direction="direction">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model.number="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="pass">
                    <el-input type="password" v-model="ruleForm.password1" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm.password2" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm()">提交</el-button>
                    <el-button @click="resetForm()">重置</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
    </div>
</template>

<script>
export default {
    naem: 'registe',
    data() {
        var checkUsername = (rule, value, callback) => {
            // console.log(value.toString().length)
            let valLength = value.toString().length
            if (!value) {
                return callback(new Error('用户名不能为空'))
            } else if (valLength < 4) {
                return callback(new Error('用户名长度不能低于4位'))
            }
        }
        var validatePass = (rule, value, callback) => {
            // 验证规则
            var reg = /^(?=.*[a-z])(?=.*[A-Z])/
            if (value === '') {
                callback(new Error('请输入密码'))
            }else if(value.length<8) {
                return callback(new Error('密码长度不能低于8位'))
            }else if(reg.test(value)===false) {
                return callback(new Error('密码必须包含英文大小写'))
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass')
                }
                callback()
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'))
            } else if (value !== this.ruleForm.password2) {
                callback(new Error('两次输入密码不一致!'))
            } else {
                callback()
            }
        };
        return {
            drawer: false,
            direction: 'rtl',
            // 用户输入
            ruleForm: {
                password1: '',
                password2: '',
                username: ''
            },
            // 校验规则
            rules: {
                password1: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                password2: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                username: [
                    { validator: checkUsername, trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        async submitForm() {         
            console.log(this.ruleForm)
            // 注册成功关闭抽屉并提示
            const res = await this.$http.post('api/register',JSON.stringify(this.ruleForm))
            console.log(res)
            this.drawer = false
            // this.$message.success('注册成功')
        },
        resetForm(){
            this.ruleForm = {
                password1: '',
                password2: '',
                username: ''
            }
        }
    }
}
</script>

<style></style>